package com.rimi.bbs.bean;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author jerry
 * @since 2019-02-24
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements UserDetails,Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "user_id",type = IdType.AUTO)
    private Integer userId;

    private String userEmail;

    private String userName;

    private String userPhone;

    private String userPassword;

    /**
     * 1.男 2女 3未知
     */
    private Integer userSex;

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField("user_createTime")
    private LocalDateTime userCreatetime;

    private String userImage;

    @TableField(fill = FieldFill.UPDATE)
    private String userAddress;

    /**
     * 1未激活 2激活
     */
    private Integer emailStatus;


    /**
     * 签名
     */
    @TableField(fill = FieldFill.UPDATE)
    private String userAutograph;

    private Role userRole;

    /**
     * 1禁用 2正常
     */
    private Integer userStatus;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //用户角色 为 普通用户
        List<GrantedAuthority> auths = new ArrayList<>();
        auths.add(new SimpleGrantedAuthority(userRole.toString()));
        return auths;
    }

    @Override
    public String getPassword() {
        return userPassword;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {//帐户是否过期
        return true;
    }

    /**
     * @return
     * 账户是未被禁用的
     */
    @Override
    public boolean isAccountNonLocked() {// 帐户是否被冻结
        return userStatus==2?true:false;
    }

    // 帐户密码是否过期，一般有的密码要求性高的系统会使用到，比较每隔一段时间就要求用户重置密码
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {// 帐号是否可用
        return true;
    }
}
